/* 
 * File:   MySQLDC.hpp
 * Author: Faisal
 *
 * Created on April 27, 2012, 8:39 PM
 *
 * Description:
 *		Data context provider for MySQL database. It provides all the functionality to connect to MySQL and run the queries.
 */

#ifndef HMySqlDC_DL

#define HMySqlDC_DL

#include "../Include/Shared.hpp"
#include "../Interfaces/DataContext.hpp"
#include "../Include/DLContract.hpp"
#include "../Include/ConnectionDefinition.hpp"
#include "../Include/ConnectionManager.hpp"

#include <Common/Include/ConnectionSourceName.hpp>
#include <Common/Include/Helper.hpp>
//#include "..\Include\DLContract.h"

#include "QueryBuilder.hpp"
#include <boost/algorithm/string/find.hpp>
#include <typeinfo>

using Supetronix::PersistO::DL::QueryBuilder;
using Supetronix::PersistO::Common::Helper;
using Supetronix::PersistO::DL::ConnectionManager;
using Supetronix::PersistO::DL::ConnectionDefinition;

using std::vector;
using std::string;

using sql::Connection;
using sql::ResultSet;
using sql::PreparedStatement;

using boost::algorithm::ifind_last;

namespace Supetronix
{
	namespace PersistO
	{
		namespace DL
		{
			class MySqlDC :
				public Supetronix::PersistO::DL::DataContext<MySqlDC, sql::Connection, sql::ResultSet, sql::PreparedStatement>//,	public DLContract
			{
				//DataContext has to be the friend class so it can access the private constructor of this class
				friend DataContext<MySqlDC, sql::Connection, sql::ResultSet, sql::PreparedStatement>;

			public:

				/*
				*	Methods
				*/

				//this is a new method from DLContract and it implements the provider specific logic to create the connection
                virtual void* createConnection(const ConnectionDefinition* connDefinition) override final;

				//business objects will call this method to execute specific query and return the resultset
				virtual ResultSet* executeQuery(ConnectionSourceName sourceName, QueryType qryType, const string& query, const QueryModifierBase* modifier) override final;

				//this is a deprecated method. New method is "createConnection" that will be called by the ConnectionManager
				virtual Connection* connect(ConnectionSourceName) override;

				/*
				*	constructor and destructor
				*/
				virtual ~MySqlDC();

			private:
				/*
				*	Methods
				*/
				//this method is used internally by the provider to prepare the sql query and replace parameter values
				virtual std::unique_ptr<sql::PreparedStatement> buildQuery(ConnectionSourceName sourceName, const string& query, const QueryModifierBase* modifier) override final;
				
				/*
				*	Constructor and Destructor
				*/
				MySqlDC();

				/*
				*	Member variables
				*/
				sql::Driver* _mySqlDriver;
			};
		}
	}
}

#endif